package com.rockcent.third;

import com.rockcent.mall.lib.domain.UserInfo;
import com.rockcent.mall.lib.dto.UserInfoDto;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * Created by oyhk on 15/11/29.
 * 用户是否登录过滤器
 */
@Component
public class LoginUserInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserInfoDto userBase = (UserInfoDto) request.getSession().getAttribute(UserInfo.LOGIN_USER);
        // 当用户session 不存在
        if (userBase == null) {
            response.sendRedirect(Routes.MAIN_USER_SIGN_IN + "?backUrl=" + request.getRequestURL());
            return false;
        }
        return super.preHandle(request, response, handler);
    }

    public List<String> getPathPatterns() {
        List<String> pathPatterns = Arrays.asList(
                Routes.MALL_SETTLED_MERCHANT,
                Routes.MALL_SETTLED,
                Routes.MALL_SETTLED_LIST,
                Routes.MALL_SETTLED_DETAIL
//                Routes.SITE+"/platform/**",
//                Routes.SITE+"**/**"
//                Routes.PLATFORM_MALL_SETTLED + "**",
//                Routes.PLATFORM_MALL_SETTLED + "**/**"

        );
        return pathPatterns;
    }
}
